from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from dhm.models.inbound_goods.inbound_goods import InboundGoods

class InboundGoodsView(APIView):
    permission_classes = ([IsAuthenticated])
    def get(self, request):
        inbound_way = request.GET.get('inbound_way', '')
        title = request.GET.get('title', '')
        inbound_time = request.GET.get('inbound_time', '')
        photo = request.GET.get('photo', '')
        describe = request.GET.get('describe', '')
        return_id = request.GET.get('return_id', '')
        inboundgoodses = InboundGoods.objects.all().order_by('-inbound_time')
        data = []
        for inboundgoods in inboundgoodses:
            data.append({
                'id': inboundgoods.id,
                'inbound_way': inboundgoods.inbound_way,
                'title': inboundgoods.title,
                'inbound_time': inboundgoods.inbound_time,
                'photo': inboundgoods.photo,
                'return_id': inboundgoods.return_id,
                'describe': inboundgoods.describe,
            })
        return Response(data)

    def post(self, request):
        data = request.POST
        inbound_way = data.get('inbound_way', '')
        title = data.get('title', '')
        inbound_time = data.get('inbound_time', '')
        photo = data.get('photo', '')
        describe = data.get('describe', '')
        return_id = data.get('return_id', '')
        InboundGoods.objects.create(inbound_way=inbound_way, title=title, inbound_time=inbound_time, photo=photo, describe=describe, return_id=return_id)
        return Response({
            'result': "success"
        })
